package com.lanren.common.helper.glidehelp;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.lanren.common.utils.ScreenUtils;

/**
 * @ClassName GlideHelper
 * @Description TODO Glide圆角、圆形、正常图片加载工具
 * @Author chongheng.wang
 * @Date 2019/8/15 13:02
 * @Version 1.0
 */
public class GlideHelper {

    /**
     * 加载图片，正常加载不做任何处理
     */
    public static void setImageResource(Context context, ImageView img, String url, @DrawableRes int errorImg) {
        Glide.with(context)
                .load(url)
                .error(errorImg)
                .placeholder(errorImg)
                .dontAnimate()//去除glide自身加载动画
                .into(img);
    }

    public static void setImageResourceMemoryCache(Context context, ImageView img, String url, @DrawableRes int errorImg,boolean skip,
                                                   DiskCacheStrategy diskCacheStrategy) {
        Glide.with(context)
                .load(url)
                .error(errorImg)
                .placeholder(errorImg)
                .dontAnimate()//去除glide自身加载动画
                .diskCacheStrategy(diskCacheStrategy)
                .skipMemoryCache(skip)
                .into(img);
    }

    public static void setImageResource(Context context, ImageView img, String url, @DrawableRes int errorImg, int crossFade) {
        Glide.with(context)
                .load(url)
                .error(errorImg)
                .placeholder(errorImg)
                .dontAnimate()//去除glide自身加载动画
                .transition(new DrawableTransitionOptions().crossFade(crossFade))//切换图片渐变效果
                .into(img);
    }

    /**
     * 设置图片为圆角，包括错误图片与占位图片
     */
    public static void setImageResourceAngle(Context context, ImageView img, String url, @DrawableRes int errorImg, float radius, boolean
            leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
        CornerTransform transformation = new CornerTransform(context, ScreenUtils.dip2px(radius, context));
        //只是绘制左上角和右上角圆角 false:绘制圆角，true:不绘制
        transformation.setExceptCorner(leftTop, rightTop, leftBottom, rightBottom);

        RequestOptions options = new RequestOptions()
                .transform(transformation);
        Glide.with(context)
                .load(url)
                .apply(options)
                .thumbnail(GlideHelper.loadTransformAngle(context, errorImg, radius, leftTop, rightTop, leftBottom, rightBottom))
                .error(GlideHelper.loadTransformAngle(context, errorImg, radius, leftTop, rightTop, leftBottom, rightBottom))
                .dontAnimate()
                .into(img);
    }

    public static void setImageResourceAngle(Context context, ImageView img, String url, @DrawableRes int errorImg, float radius, boolean
            leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom, int crossFade) {
        CornerTransform transformation = new CornerTransform(context, ScreenUtils.dip2px(radius, context));
        //只是绘制左上角和右上角圆角 false:绘制圆角，true:不绘制
        transformation.setExceptCorner(leftTop, rightTop, leftBottom, rightBottom);

        RequestOptions options = new RequestOptions()
                .transform(transformation);
        Glide.with(context)
                .load(url)
                .apply(options)
                .thumbnail(GlideHelper.loadTransformAngle(context, errorImg, radius, leftTop, rightTop, leftBottom, rightBottom))
                .error(GlideHelper.loadTransformAngle(context, errorImg, radius, leftTop, rightTop, leftBottom, rightBottom))
                .dontAnimate()
                .transition(new DrawableTransitionOptions().crossFade(crossFade))//切换图片渐变效果
                .into(img);
    }


    private static RequestBuilder<Drawable> loadTransformAngle(Context context, @DrawableRes int placeholderId, float radius, boolean leftTop,
                                                               boolean rightTop, boolean leftBottom, boolean rightBottom) {
        CornerTransform transformation = new CornerTransform(context, ScreenUtils.dip2px(radius, context));
        //只是绘制左上角和右上角圆角 false:绘制圆角，true:不绘制
        transformation.setExceptCorner(leftTop, rightTop, leftBottom, rightBottom);
        RequestOptions options = new RequestOptions()
                .transform(transformation);
        return Glide.with(context)
                .load(placeholderId)
                .apply(options);
    }


    /**
     * 设置图片为圆形，包括错误图片与占位图片
     */
    public static void setImageResourceGarden(Context context, ImageView img, String url, @DrawableRes int errorImg) {
        RequestOptions options = new RequestOptions()
                .circleCrop();
        Glide.with(context)
                .load(url)
                .apply(options)
                .thumbnail(GlideHelper.loadTransformGarden(context, errorImg))
                .error(GlideHelper.loadTransformGarden(context, errorImg))
                .dontAnimate()
                .into(img);
    }

    public static void setImageResourceGarden(Context context, ImageView img, String url, @DrawableRes int errorImg, int crossFade) {
        RequestOptions options = new RequestOptions()
                .circleCrop();
        Glide.with(context)
                .load(url)
                .apply(options)
                .thumbnail(GlideHelper.loadTransformGarden(context, errorImg))
                .error(GlideHelper.loadTransformGarden(context, errorImg))
                .transition(new DrawableTransitionOptions().crossFade(crossFade))//切换图片渐变效果
                .dontAnimate()
                .into(img);
    }


    private static RequestBuilder<Drawable> loadTransformGarden(Context context, @DrawableRes int placeholderId) {

        RequestOptions options = new RequestOptions()
                .circleCrop();
        return Glide.with(context)
                .load(placeholderId)
                .apply(options);
    }


}
